package org.shoukaiseki.aqoeous.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;

import javax.validation.constraints.NotNull;

/**
 * org.shoukaiseki.aqoeous.config.SpringContextUtil
 * <br>
 *
 * @author 蒋カイセキ    Japan-Tokyo  19-1-31 下午7:29<br>
 * ブログ http://www.shoukaiseki.top/<br>
 * E-メール jiang28555@Gmail.com<br>
 **/
@Configuration
public class SpringContextUtil implements ApplicationContextAware {

    // Spring应用上下文环境
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }


    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }


    /**
     *
     * @Description TODO: 获取bean
     * @Author Rick
     * @Param: name beanName
     * @Date 2018/10/10 16:23
     */
    public static Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);
    }

    /**
     *
     * @Description TODO: 获取bean
     * @Author Rick
     * @Param: name beanName
     * @Date 2018/10/10 16:23
     */
    public static <T> T getBean(String name,@Nullable Class<T> clazz) throws BeansException {
        return applicationContext.getBean(name,clazz);
    }
}

